package com.manlost.upload.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.manlost.biz.baby.model.constants.BabyType;
import com.manlost.upload.model.constant.AblumConstant;
import com.manlost.upload.model.dataobject.AblumFolder;
import com.manlost.upload.model.dataobject.LocalBabyInfo;
import com.manlost.upload.operate.AblumFolderOp;

/**
 * 类AblumFolderUtil.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-5-6 上午10:20:34
 */
public class AblumFolderUtil {

    private static final String[] PIC   = new String[] { "bmp", "dib", "gif", "jfif", "jpe", "jpeg", "jpg", "png",
            "tif", "tiff", "ico"       };
    private static final String[] VIDEO = new String[] { "avi", "mov", "asf", "wmv", "rm", "rmvb", "mkv", "flv" };

    @SuppressWarnings("unchecked")
    public static List<AblumFolder> listAblumFolders(String uploadFolderPath) {
        File uploadFolder = new File(uploadFolderPath);
        if (!uploadFolder.exists() || !uploadFolder.isDirectory()) {
            return Collections.EMPTY_LIST;
        }

        List<AblumFolder> ablumFolders = new ArrayList<AblumFolder>();
        AblumFolderOp operate = new AblumFolderOp(uploadFolder);
        Set<String> dirNames = operate.getDirNames();
        if (dirNames != null && dirNames.size() > 0) {
            Iterator<String> it = dirNames.iterator();
            while (it.hasNext()) {
                AblumFolder ablum = generateAblumFolder(it.next());
                if (ablum != null) {
                    ablumFolders.add(ablum);
                }
            }
        }
        return ablumFolders;
    }

    public static AblumFolder generateAblumFolder(String filepath) {
        if (StringUtils.isBlank(filepath)) {
            return null;
        }
        String[] infos = filepath.split(File.separator);
        if (infos.length - AblumConstant.defaultPathLength < 5) {
            return null;
        }
        File ablumFolder = new File(filepath);
        if (!ablumFolder.exists() || !ablumFolder.isDirectory()) {
            return null;
        }

        AblumFolder ablum = new AblumFolder();
        List<LocalBabyInfo> localPicBabyInfos = new ArrayList<LocalBabyInfo>();
        List<LocalBabyInfo> localVideoBabyInfos = new ArrayList<LocalBabyInfo>();

        Integer picCount = 0;
        Integer videoCount = 0;

        ablum.setAbsolutePath(filepath);
        // 遍历专辑，保存每个本地文件的File信息
        for (File file : ablumFolder.listFiles()) {
            // 判断是否上传
            if (file.getName().equals(AblumConstant.IS_UPLOAD_FILENAME)) {
                ablum.setIsUploaded(Boolean.TRUE);
                continue;
            } else {
                ablum.setIsUploaded(Boolean.FALSE);
            }
            // 判断是否是图片
            for (String picFix : PIC) {
                if (file.getName().toLowerCase().endsWith(picFix)) {
                    picCount++;
                    LocalBabyInfo info = new LocalBabyInfo();
                    info.setLocalFileName(file.getName());
                    info.setBabyType(BabyType.PIC.getValue());
                    info.setLocalAbsolutePath(file.getAbsolutePath());
                    localPicBabyInfos.add(info);
                    break;
                }
            }
            // 判断是否是视频
            for (String videoFix : VIDEO) {
                if (file.getName().toLowerCase().endsWith(videoFix)) {
                    videoCount++;
                    LocalBabyInfo info = new LocalBabyInfo();
                    info.setLocalFileName(file.getName());
                    info.setBabyType(BabyType.VIDEO.getValue());
                    info.setLocalAbsolutePath(file.getAbsolutePath());
                    localVideoBabyInfos.add(info);
                    break;
                }
            }
        }

        ablum.setYear(infos[infos.length - 5]);
        ablum.setMonDay(infos[infos.length - 4]);
        ablum.setSource(infos[infos.length - 3]);
        ablum.setAuthor(infos[infos.length - 2]);
        ablum.setAlbum(infos[infos.length - 1]);
        ablum.setPicCount(picCount);
        ablum.setVideoCount(videoCount);
        ablum.setLocalPicBabyInfos(localPicBabyInfos);
        ablum.setLocalVideoBabyInfos(localVideoBabyInfos);
        ablum.setAbsolutePath(filepath);
        return ablum;
    }
}
